package controller

import (
	"go-web-mini/common"
	"go-web-mini/repository"
	"go-web-mini/response"
	"go-web-mini/vo"

	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
)

type ITodoController interface {
	GetList(c *gin.Context) // 获取列表

}

type TodoController struct {
	TodoRepository repository.TodoRepository
}

// 构造函数
func NewTodoController() ITodoController {
	TodoRepository := repository.NewTodoRepository()
	TodoController := TodoController{TodoRepository: TodoRepository}
	return TodoController
}

// 获取用户列表
func (uc TodoController) GetList(c *gin.Context) {
	var req vo.TodoRequest
	// 参数绑定
	if err := c.ShouldBind(&req); err != nil {
		response.Fail(c, nil, err.Error())
		return
	}
	// 参数校验
	if err := common.Validate.Struct(&req); err != nil {
		errStr := err.(validator.ValidationErrors)[0].Translate(common.Trans)
		response.Fail(c, nil, errStr)
		return
	}

	// 获取
	Todos, total, err := uc.TodoRepository.GetList(&req)
	if err != nil {
		response.Fail(c, nil, "获取用户列表失败: "+err.Error())
		return
	}
	response.Success(c, gin.H{"Todos": Todos, "total": total}, "获取用户列表成功")
}
